// miniprogram/pages/information/information.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:"没识别"
  },
  createTwo: function () {
    wx.reLaunch({
      url: '../createTwoCode/createTwoCode'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
 
  //拍照或者从相册选择要识别的照片
  photo(type) {
    let that = this
    
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'], ///'original',压缩图
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        let imgUrl = res.tempFilePaths[0];
        console.log("photo", imgUrl)
        that.uploadImg("11", imgUrl)
      }
    })
  },

  // 上传图片到云存储
  uploadImg(type, imgUrl) {
    let that = this
    wx.cloud.uploadFile({
      cloudPath: 'ocr/' + type + '.png',
      filePath: imgUrl, // 文件路径
      success: res => {
        console.log("上传成功", res.fileID)
        that.getImgUrl(type, res.fileID)
      },
      fail: err => {
        console.log("上传失败", err)
      }
    })
  },

  //获取云存储里的图片url
  getImgUrl(type, imgUrl) {
    let that = this
    wx.cloud.getTempFileURL({
      fileList: [imgUrl],
      success: res => {
        let imgUrl = res.fileList[0].tempFileURL
        console.log("获取图片url成功", imgUrl)
        that.shibie(type, imgUrl)
      },
      fail: err => {
        console.log("获取图片url失败", err)
      }
    })
  },

  //调用云函数，实现OCR识别
  shibie(type, imgUrl) {
    // console.log("识别成功shibie", type)
    let that =this
    wx.cloud.callFunction({
      name: "photo",
      data: {
        type: "shenfenzheng",
        imgUrl: imgUrl
      },
      success(res) {
        console.log("识别成功", res)
        that.setData({
          name: res.result.name
        })
      },
      fail(res) {
        console.log("识别失败", res)
      }
    })
    console.log("识别成功res.result.name" )
    
    
  }


})